package com.jhd.fuzhou.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.util.GetImageByUrl;
import com.jhd.fuzhou.view.photo.ShowPhotoActivity;
import com.jhd.fuzhou.vo.SuperviseEnclosureVo;
import com.jhd.fuzhou.vo.SupervisionInspectionPhotoVo;
import com.jhd.fuzhou.vo.SupervisionInspectionVo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.ViewHolder> {
    private List<SupervisionInspectionPhotoVo> supervisionInspectionPhotoVoList;private String supervisionType;
    private SupervisionInspectionVo supervisionInspectionVo;
    private String photoTemplateUrl;
    GetImageByUrl getImageByUrl = new GetImageByUrl();
    Gson gson=new Gson();
    private Context context;
    public PhotoAdapter(Context context, SupervisionInspectionVo supervisionInspectionVo, List<SupervisionInspectionPhotoVo> supervisionInspectionPhotoVoList, String supervisionType, String photoTemplateUrl) {
        this.supervisionInspectionVo = supervisionInspectionVo;
        this.supervisionInspectionPhotoVoList = supervisionInspectionPhotoVoList;
        this.supervisionType = supervisionType;
        this.context = context;
        this.photoTemplateUrl = photoTemplateUrl;
    }
    @NonNull
    @Override
    public PhotoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, parent, false);
        PhotoAdapter.ViewHolder viewHolder = new PhotoAdapter.ViewHolder(itemView);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(@NonNull PhotoAdapter.ViewHolder holder, int position) {
        holder.tv_name.setText(supervisionInspectionPhotoVoList.get(position).getPhotoNum());
        String path=context.getExternalFilesDir(null).getPath() + "/Download/photo/"+supervisionInspectionPhotoVoList.get(position).getPhotoNum()+".png";
        if((new File(path).exists())) {
            holder.image.setImageBitmap(BitmapFactory.decodeFile(path));
        }else{
            getImageByUrl.setImage(holder.image, supervisionInspectionPhotoVoList.get(position).getPhotoUrl());
        }
        holder.relativeLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("删除");
                builder.setMessage("你确定删除该现场图片证据吗");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        List<SuperviseEnclosureVo> superviseEnclosureVoList;
                        if(supervisionType.equals("专项监督")){
                            superviseEnclosureVoList= supervisionInspectionVo.getDocumentAttachmentList();
                        }else{
                            superviseEnclosureVoList= supervisionInspectionVo.getSuperviseEnclosureVoList();
                        }
                        if(superviseEnclosureVoList!=null){
                            for (SuperviseEnclosureVo superviseEnclosureVo : superviseEnclosureVoList) {
                                if(superviseEnclosureVo.getEnclosureName().equals("现场照片证据"+supervisionInspectionPhotoVoList.get(position).getPhotoNum())){
                                    superviseEnclosureVoList.remove(superviseEnclosureVo);
                                    (new File(path)).delete();
                                    break;
                                }
                            }
                        }
                        if(supervisionType.equals("专项监督")){
                            supervisionInspectionVo.setDocumentAttachmentList(superviseEnclosureVoList);
                        }else{
                            supervisionInspectionVo.setSuperviseEnclosureVoList(superviseEnclosureVoList);
                        }
                        supervisionInspectionPhotoVoList.remove(position);
                        notifyDataSetChanged();
                        supervisionInspectionVo.setSupervisionInspectionPhotoVoList(supervisionInspectionPhotoVoList);//删除照片
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                Dialog dialog = builder.create();
                if (!((Activity) context).isFinishing()) dialog.show();

                int color = context.getResources().getColor(R.color.header);
                Button btn = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
                Button btn2 = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
                if (btn != null) btn.setTextColor(color);
                if (btn2 != null) btn2.setTextColor(color);
                return false;
            }
        });
        holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, ShowPhotoActivity.class);
                intent.putExtra("data",gson.toJson(supervisionInspectionPhotoVoList.get(position)));
                intent.putExtra("position",String.valueOf(position));
                intent.putExtra("supervisionType",supervisionType);
                context.startActivity(intent);
            }
        });
    }
    @Override
    public int getItemCount() {
        return supervisionInspectionPhotoVoList.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView image;
        RelativeLayout relativeLayout;
        public ViewHolder(View itemView) {
            super(itemView);
            this.tv_name = itemView.findViewById(R.id.tv_name);
            this.image = itemView.findViewById(R.id.image);
            this.relativeLayout = itemView.findViewById(R.id.relativeLayout);
        }
    }
}
